import pygame
import random
import time
fp="python3/study/02.txt"
s=00
s=str(s)
f=open(fp,"w+")
f.writelines(s)
f.close()
def draw(x,y,z):
    #a=random.randint(0,255)
    #b=random.randint(0,255)
    #c=random.randint(0,255)
    a=255
    b=0
    c=0
    for i in range(0,z):
        if i==0:
            pygame.draw.circle(screen,[a,b,c],[x[i],y[i]],27,0)
        if i!=0:
            pygame.draw.circle(screen,[a,b,c],[x[i],y[i]],23,0)
pygame.init()

def drawo(x,y,z):
    for i in range(0,z):
        pygame.draw.circle(screen,[255,255,255],[x[i],y[i]],27,0)
pygame.init()

def eatx(x,y,z):
    c=1
    exy=[]
    for ii in range(0,10):
        for iii in range(0,10):
            e_x=ii
            e_y=iii
            e_xy=str(e_x)+str(e_y)
            exy.append(e_xy)
    for i in range(0,z+1):
        x_1=int((x[i]-30)/60)
        y_1=int((y[i]-30)/60)
        x_1=str(x_1)
        y_1=str(y_1)
        xy=x_1+y_1
        if xy in exy:
            exy.remove(xy)
    r=True
    while r:
        if c<10:
            xy=str(random.randint(0,99))
            if xy in exy:
                r=False
            else:
                c+=1
        if c>9:
            fp="python3/study/02.txt"
            f=open(fp,"r+")
            xy=f.readline(1000)
            if xy in exy:
                r=False
            else:
                xy=int(xy)
                xy+=1
                if xy>99:
                    xy="00"
                xy=str(xy)
                f=open(fp,"w+")
                s=str(xy)
                f.writelines(s)
                f.close()
    return xy

def shu(fx):
    a_1=fx[0]+fx[1]+fx[2]+fx[3]
    if a_1<1:
        print("你输啦")
        print(f"分数:{fenshu}")
        time.sleep(2)
        running=False
        return False
    else:
        return True

pygame.display.set_caption("贪吃蛇 good luck")
screen=pygame.display.set_mode([600,600])
screen.fill([255,255,255])

x=[30]
y=[30]
z=1
z_1=100
"""
zuo=True
you=True
shang=True
xia=True
"""
fx=[1,1,1,1]
for iii in range(0,z_1):
    x.append(0)
    y.append(0)
fenshu=0

exy=eatx(x,y,z)


def draweat(x):
    z=0
    for i in x:
        if z==0:
            ex=i
            z+=1
        if z!=0:
            ey=i
    x=int(ex)
    y=int(ey)
    x=x*60+30
    y=y*60+30
    #pygame.draw.circle(screen,[0,0,0],[x+25,y-25],25,0)
    #pygame.draw.circle(screen,[255,255,255],[x+25,y-25],24,0)
    #pygame.draw.rect(screen,[255,255,255],[x+45,y-50,5,50],50)
    pygame.draw.circle(screen,[255,0,255],[x,y],23,0)
    pygame.display.flip()
draweat(exy)
pygame.display.flip()

for chang in range(0,10):
    chang = chang*60
    for kuan in range(0,10):
        kuan = kuan*60
        pygame.draw.rect(screen,[0,255,0],[chang,kuan,60,60],1)
        pygame.display.flip()

def fxt(x,y,fx):
    if y[0]-60<=0:
        fx[0]=0
    if y[0]-60>0:
        fx[0]=1

    if y[0]+60>=600:
        fx[1]=0
    if y[0]+60<600:
        fx[1]=1

    if x[0]-60<=0:
        fx[2]=0
    if x[0]-60>0:
        fx[2]=1

    if x[0]+60>=600:
        fx[3]=0
    if x[0]+60<600:
        fx[3]=1
    return fx

def fxpd(x,y,z,fx):
    for i in range(1,z):
        if fx[0]==1:
            if y[0]-60==y[i]:
                if x[0]==x[i]:
                    fx[0]=0
                    continue
            if y[0]-60!=y[i]:
                fx[0]=1
        if fx[1]==1:
            if y[0]+60==y[i]:
                if x[0]==x[i]:
                    fx[1]=0
                    continue
            if y[0]+60!=y[i]:
                fx[1]=1
        if fx[2]==1:
            if x[0]-60==x[i]:
                if y[0]==y[i]:
                    fx[2]=0
                    continue
            if x[0]-60!=x[i]:
                fx[2]=1
        if fx[3]==1:
            if x[0]+60==x[i]:
                if y[0]==y[i]:
                    fx[3]=0
                    continue
            if x[0]+60!=x[i]:
                fx[3]=1
    return fx

draw(x,y,z)
pygame.display.flip()
running=True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running=False
        elif event.type == pygame.KEYDOWN:
            #pygame.draw.circle(screen, [255,255,255], [x,y],25)
            #print(event.key)
            draweat(exy)
            draw(x,y,z)
            pygame.display.flip()
            running=shu(fx)

            fxt(x,y,fx)
            fxpd(x,y,z,fx)
                
            if event.key == pygame.K_UP:
                #print("向上")
                #if y[0]-60>0:
                if fx[0]==1:
                    drawo(x,y,z)
                    pygame.display.flip()
                    x_1=x[0]
                    y_1=y[0]-60
                    x.insert(0,x_1)
                    y.insert(0,y_1)
                    del x[z_1]
                    del y[z_1]
            elif event.key == pygame.K_DOWN:
                #print("向下")
                #if y[0]+60<600:
                if fx[1]==1:
                    drawo(x,y,z)
                    pygame.display.flip()
                    x_1=x[0]
                    y_1=y[0]+60
                    x.insert(0,x_1)
                    y.insert(0,y_1)
                    del x[z_1]
                    del y[z_1]
            elif event.key == pygame.K_LEFT:
                #print("向左")
                #if x[0]-60>0:
                if fx[2]==1:
                    drawo(x,y,z)
                    pygame.display.flip()
                    #for i in range(0,z):                         
                     #   x[i]-=1*60
                    x_1=x[0]-60
                    y_1=y[0]
                    x.insert(0,x_1)
                    y.insert(0,y_1)
                    del x[z_1]
                    del y[z_1]
            elif event.key == pygame.K_RIGHT:
                #print("向右")
                #if x[0]+60<600:
                if fx[3]==1:
                    drawo(x,y,z)
                    pygame.display.flip()
                    x_1=x[0]+60
                    y_1=y[0]
                    x.insert(0,x_1)
                    y.insert(0,y_1)
                    del x[z_1]
                    del y[z_1]
            sxy=str(int((x[0]-30)/60))+str(int((y[0]-30)/60))
            draweat(exy)
            draw(x,y,z)
            pygame.display.flip()
            #print(sxy,exy)
            if sxy==exy:
                fenshu+=1
                #print(fenshu)
                exy=eatx(x,y,z)
                z+=1
                if z>=z_1:
                    print("你赢了")
                    print(f"分数:{fenshu}")
                    running=False
                    time.sleep(1)
            running=shu(fx)
            draweat(exy)
            draw(x,y,z)
            pygame.display.flip()

pygame.quit
pygame.quit()


"""
def eatx(x,z):
    e_x=random.randint(0,9)
    e_x=e_x*60
    e_x+=30
    r=True
    i=1
    z+=1
    while r:
        r=False
        i=1
        e_x=random.randint(0,9)
        e_x=e_x*60
        e_x+=30
        
        while i < z:
            if x[i]==e_x:
                r=True
            i+=1
        
        for a in range[i,z]:
            if x[a]==e_x:
                r=True

    return e_x

def eaty(y,z):
    e_y=random.randint(0,9)
    e_y=e_y*60
    e_y+=30
    r=True
    while r:
        r=False
        i=1
        e_y=random.randint(0,9)
        e_y=e_y*60
        e_y+=30
        
        while i < z:
            if y[i]==e_y:
                r=True
            i+=1
       
        for a in range[i,z+1]:
            if y[a]==e_y:
                r=True
    return e_y
    
    for i in range(0,z):
        if y[i]==e_y:
            e_y=random.randint(0,9)
            e_y=e_y*60
            e_y+=30
    for i in range(0,z):
        if y[i]==e_y:
            r=True
        else:
            r=False
        
    return e_y

def eatx(x,y,z):
    exy=[]
    for ii in range(0,10):
        for iii in range(0,10):
            e_x=ii
            e_y=iii
            e_xy=str(e_x)+str(e_y)
            exy.append(e_xy)
    for i in range(0,z):
        x_1=int((x[i]-30)/60)
        y_1=int((y[i]-30)/60)
        x_1=str(x_1)
        y_1=str(y_1)
        xy=x_1+y_1
        if xy in exy:
            exy.remove(xy)
    r=True
    while r:
        xy=str(random.randint(0,99))
        if xy in exy:
            r=False
        else:
            pass
    return xy
fp="python3/study/base.txt"
f=open(fp,"r+")
c_x=int(f.readline(100))
c_y=int(f.readline(100))
f=open(fp,"w+")
s=str(c_x)
s2=str(c_y)
f.writelines([s+"\n",s2+"\n"])
f.close()
"""
#zgy=["翟光亚","45","100"]
#zgy[1]=46
#class ren {
#  def __init__(name,year,weith):
#      self.name=name
#      self.year=year
#      self.weight=weight
#}
# zgy=ren("翟光亚",45,"100")
# zgy.year=46
#class node {
#  def __init__(x,y):
#      self.x=x
#      self.y=y
#  def move(self,xshift,yshift):
#      self.x+=xshift
#      self.y+=yshift
#}
# snake=[node(1,1),node(1,2),node(1,3),node(1,4),node(1,5)]
# for i in snake:
#     snake[i].move(1,0)